package com.xiaoniu.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoniu.dto.UserQueryDto;
import com.xiaoniu.model.sys.SysUser;
import com.xiaoniu.result.Result;
import com.xiaoniu.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zhangjianshan on 2021-05-04
 */
@Api(tags = "用户管理")
@RestController
@Value
@RequiredArgsConstructor
@RequestMapping("/admin/sys/")
@CrossOrigin
public class UserController {

    UserService userService;

    @ApiOperation("获取用户列表")
    @PostMapping("userList/{pageNo}/{pageSize}")
    public Result querySysUserList(@PathVariable int pageNo,
                                   @PathVariable int pageSize,
                                   @RequestBody UserQueryDto userQueryDto) {
        Page<SysUser> sysUserList = userService.queryPageUserList(pageNo, pageSize, userQueryDto);
        return Result.ok(sysUserList);
    }

    @ApiOperation("逻辑删除用户")
    @DeleteMapping("{userId}")
    public Result deleteSysUser(@PathVariable Long userId) {
        boolean flag = userService.removeById(userId);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("批量逻辑删除用户")
    @DeleteMapping("bathDelete")
    public Result bathDeleteSysUser(@RequestBody List<Long> userIdList) {
        boolean flag = userService.removeByIds(userIdList);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation("禁用/启用")
    @PutMapping("updateSysUserStatus/{userId}/{userStatus}")
    public Result updateSysUserStatus(@PathVariable Long userId,
                                      @PathVariable Boolean userStatus) {
        SysUser sysUser = userService.getById(userId);
        if (sysUser != null) {
            sysUser.setUserStatus(userStatus);
            return Result.ok(userService.updateById(sysUser));
        }
        return Result.fail();
    }

    @ApiOperation("新增用户")
    @PostMapping("createSysUser")
    public Result createSysUser(@RequestBody SysUser sysUser) {
        if (sysUser.getUserId() == null) {
            sysUser.setPassword("1");
        }
        userService.saveOrUpdate(sysUser);
        return Result.ok();
    }

    @ApiOperation("根据id用户")
    @GetMapping("getSysUserById/{userId}")
    public Result getSysUserById(@PathVariable Long userId) {
        return Result.ok(userService.getById(userId));
    }


    @ApiOperation("导出用户")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response) {
        userService.download(response);
    }

    @ApiOperation("导入用户")
    @PostMapping("/importUser")
    public Result importUser(MultipartFile file) {
        userService.importUser(file);
        return Result.ok();
    }
}
